package cvo.pro4.chess.client.entities.pieces;


import java.util.ArrayList;
import java.util.List;

import cvo.pro4.chess.client.entities.BoardCoordinates;
import cvo.pro4.chess.client.entities.Color;
import cvo.pro4.chess.client.entities.Game;
import cvo.pro4.chess.client.entities.Piece;
import cvo.pro4.chess.client.exceptions.IllegalCoordinatesException;

public class Pawn extends Piece{
	
	public  Pawn(Color color){
		super(color);
		setName("Pawn");
	}

	@Override
	public void move(BoardCoordinates coordinates) {
		// TODO Auto-generated method stub
		
	}

	public List<BoardCoordinates> getPossibleMoves() throws IllegalCoordinatesException {
//      
		// ============================================================================================
		// variabelen 
		// ============================================================================================
			List<BoardCoordinates> possibleMoves = new ArrayList<BoardCoordinates>();
			BoardCoordinates currentPosition = this.getCurrentPosition();
			BoardCoordinates possiblePosition = null;
			BoardCoordinates skippedPosition = null;
			  
		// ============================================================================================
		// move pawn 1 place forward
		// ============================================================================================
			switch(this.getColor()){
			case WHITE:  
				try {
					possiblePosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()+1);
				} catch (IllegalCoordinatesException e) {
					System.out.println(e.toString() + possiblePosition.toString());
				}
				if(Game.getGame().getBoard().getPieceAtLocation(possiblePosition) == null){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//			if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
						possibleMoves.add(possiblePosition);
		//			}
				}
			case BLACK: 
				try {
					possiblePosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()-1);
				} catch (IllegalCoordinatesException e) {
					System.out.println(e.toString() + possiblePosition.toString());
				}
				if(Game.getGame().getBoard().getPieceAtLocation(possiblePosition) == null){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//			if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
						possibleMoves.add(possiblePosition);
		//			}
				}
			}

		// ============================================================================================
		// move pawn 2 places forward at start
		// ============================================================================================
			switch(this.getColor()){
			case WHITE:  
				try {
					if(currentPosition.getY()==1){	// 1 staat voor de 2de rij op het bord (=start positie van witte pion)
						possiblePosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()+2);
						skippedPosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()+1);
					}
				} catch (IllegalCoordinatesException e) {
					System.out.println(e.toString() + possiblePosition.toString());
				}
				if(Game.getGame().getBoard().getPieceAtLocation(skippedPosition) == null && 
					Game.getGame().getBoard().getPieceAtLocation(possiblePosition) == null){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//			if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
						possibleMoves.add(possiblePosition);
		//			}
				}
			case BLACK: 
				try {
					if(currentPosition.getY()==6){	// 6 staat voor de 7de rij op het bord (=start positie van zwarte pion)!
						possiblePosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()-2);
						skippedPosition = BoardCoordinates.create(currentPosition.getX(), currentPosition.getY()-1);
					}
				} catch (IllegalCoordinatesException e) {
					System.out.println(e.toString() + possiblePosition.toString());
				}
				if(Game.getGame().getBoard().getPieceAtLocation(skippedPosition) == null && 
					Game.getGame().getBoard().getPieceAtLocation(possiblePosition) == null){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//			if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
						possibleMoves.add(possiblePosition);
		//			}
				}
			}
		  

		// ============================================================================================
		// pawn takes other piece
		// ============================================================================================
			for (int i=-1;i<2;i+=2){
				switch(this.getColor()){
				case WHITE: 
					possiblePosition = BoardCoordinates.create(currentPosition.getX()+i, currentPosition.getY()+1);
//					try {
//						possiblePosition = BoardCoordinates.create(currentPosition.getX()+i, currentPosition.getY()+1);
//					} catch (IllegalCoordinatesException e) {
//						System.out.println(e.toString() + possiblePosition.toString());
//					}
					
					if(Game.getGame().getBoard().getPieceAtLocation(possiblePosition) != null && 
						Game.getGame().getBoard().getPieceAtLocation(possiblePosition).getColor()!=this.getColor() ){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//				if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
							possibleMoves.add(possiblePosition);
		//				}
					}
				case BLACK:
					try {
						possiblePosition = BoardCoordinates.create(currentPosition.getX()+i, currentPosition.getY()-1);
					} catch (IllegalCoordinatesException e) {
						System.out.println(e.toString() + possiblePosition.toString());
					}
					
					if(Game.getGame().getBoard().getPieceAtLocation(possiblePosition)!=null && Game.getGame().getBoard().getPieceAtLocation(possiblePosition).getColor()!=this.getColor()){
		// TODO: King || Army || Board  moet methode isKingChecked(Color color) hebben die Boolean teruggeeft.
		//				if(!Game.getGame().getBoard().isKingChecked(this.getColor())){
							possibleMoves.add(possiblePosition);
		//				}
					}
				}
			}
			
		// ============================================================================================
		// move pawn "en-passant"
		// ============================================================================================

		// TODO: methode voor en-passant slaan nog uittekenen
			
//			============================================================================================
//			return List of possible moves
//			============================================================================================
//			return null;
			return possibleMoves;
	}

}
